package com.culture.bootdemo.算法.meituan;

/**
 * @author: wen
 * @date 2020/10/23
 * 判断字符串内容是否相同，乱序也算
 */
public class 字符串内容是否相同 {
    private static void reverseCharArr(char[] arr, int left, int right) {
        if (arr == null || left > right) return;
        while (left < right) {
            char tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

    private static String rotate(String str, int i) {
        if (str == null || i > str.length()) return null;

        char arr[] = str.toCharArray();
        reverseCharArr(arr, 0, i - 1);
        reverseCharArr(arr, i, arr.length - 1);
        reverseCharArr(arr, 0, arr.length - 1);

        return new String(arr);
    }

    public static boolean isRotate(String s1, String s2) {
        if (s1 == null || s2 == null || s2.length() != s1.length()) return false;
        int indexOfFirst = s2.indexOf(s1.charAt(0));
        if (indexOfFirst == -1) return false;
        String tmp = rotate(s2, indexOfFirst);
        if (tmp.equals(s1)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(isRotate("ABCD", "DABCD"));

    }
}
